package com.youhome.mobile.controller;

import com.youhome.common.exception.CustomException;
import com.youhome.common.response.Result;
import com.youhome.common.response.ResultCode;
import com.youhome.common.response.ResultPage;
import com.youhome.mobile.dto.DTOHouseSearch;
import com.youhome.service.model.ListHubList;
import com.youhome.service.parameter.ParamHouseSearch;
import com.youhome.service.response.AssociateResult;
import com.youhome.service.service.ListHubPhotoService;
import com.youhome.service.service.SolrService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by chen365 on 2018/09/13.
 */
@Api(tags = "房源")
@RestController
@RequestMapping(value = "/houses")
public class HouseController {

    @Value("${oss.domain}")
    private String OSS_DOMAIN;

    @Autowired
    private SolrService solrService;

    @Autowired
    private ListHubPhotoService listHubPhotoService;


    @ApiOperation(value = "房源搜索", notes = "房源搜索", response = ListHubList.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping("/search")
    public Object search(@ApiParam @Validated DTOHouseSearch dtoHouseSearch) throws CustomException {
        ModelMapper modelMapper = new ModelMapper();

        return new ResultPage(ResultCode.SUCCESS, solrService.searchByBean(modelMapper.map(dtoHouseSearch, ParamHouseSearch.class)));
//        try {
//            long btime = System.currentTimeMillis();
//            if (!StringUtils.isEmpty(action) && !StringUtils.isEmpty(keyword)) {
//                if (action.equalsIgnoreCase("zip")) {
//                    if (keyword.length() != 5) {
//                        return new ResultPage(ResultCode.HOUSE_ILLEGALITY_ZIP_CODE);
//                    }
//                } else if (action.equalsIgnoreCase("address")) {
//                    if (keyword.length() < 3) {
//                        return new ResultPage(ResultCode.HOUSE_ADDRESS_LENGTH_SHORTAGE);
//                    }
//                } else if (action.equalsIgnoreCase("city")) {
//                    if (keyword.length() < 3) {
//                        return new ResultPage(ResultCode.HOUSE_CITY_LENGTH_SHORTAGE);
//                    }
//                } else {
//                    return new ResultPage(ResultCode.UNSUPPORTED_SEARCH_TYPE);
//                }
//            }
//
//            String status = "Active";
//            Boolean isCheck = null;
//            ListHubParam params = null;
//
//            //查询自有房源
//            Long companyId = null;
//            if (StringUtils.isEmpty(page) || page < 1) {
//                page = 1;
//            }
//
//            Double minX = null;
//            Double maxX = null;
//            Double minY = null;
//            Double maxY = null;
//            Boolean isStar = null;
//            PageInfo<ListHubList> listHubLists = solrService.searchByBean(
//                    action, keyword, page, pageSize, type, beds, baths, year,
//                    minSize, maxSize, minPrice, maxPrice, filed, direction, isStar,
//                    isCheck, minX, maxX, minY, maxY, companyId, null, null);
//
//            long etime = System.currentTimeMillis();
//
//            System.err.println("执行时间：" + (etime - btime) + "ms");
//
//            return new ResultPage(ResultCode.SUCCESS, listHubLists);
//
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new ResultPage(ResultCode.FAIL);
//        }
    }

    @ApiOperation(value = "搜索联想", notes = "搜索联想", response = AssociateResult.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParam(dataType = "String", name = "keyword", value = "搜索关键字,三个字符以上", required = true, paramType = "query")
    @GetMapping(value = "/typeahead")
    public Object typeahead(String keyword) throws CustomException {
        return new Result(ResultCode.SUCCESS, solrService.associateSearch(keyword));
    }

    @ApiOperation(value = "房源图片", notes = "房源图片", response = String.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "path")
    @GetMapping(value = "/{houseId}/photos")
    public Object photos(@PathVariable String houseId) throws CustomException {
        return new Result(ResultCode.SUCCESS, listHubPhotoService.selectPhotoOfMobile(houseId));
    }

}
